home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / ui / drawing / example / AnimatorApplet.java < prev    next >
Encoding:
Java Source  |  1997-07-13  |  3.1 KB  |  106 lines

  1. /*
  2.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. import java.awt.*;
  18. import java.applet.Applet;
  19.  
  20. /* 
  21.  * Based on Arthur van Hoff's animation examples, this applet
  22.  * can serve as a template for all animation applets.
  23.  */
  24.  
  25. public class AnimatorApplet extends Applet implements Runnable {
  26.     int frameNumber = -1;
  27.     int delay;
  28.     Thread animatorThread;
  29.     boolean frozen = false;
  30.  
  31.     public void init() {
  32.         String str;
  33.         int fps = 10;
  34.  
  35.         //How many milliseconds between frames?
  36.         str = getParameter("fps");
  37.         try {
  38.             if (str != null) {
  39.                 fps = Integer.parseInt(str);
  40.             }
  41.         } catch (Exception e) {}
  42.         delay = (fps > 0) ? (1000 / fps) : 100;
  43.     }
  44.  
  45.     public void start() {
  46.         if (frozen) { 
  47.             //Do nothing.  The user has requested that we 
  48.             //stop changing the image.
  49.         } else {
  50.             //Start animating!
  51.             if (animatorThread == null) {
  52.                 animatorThread = new Thread(this);
  53.             }
  54.             animatorThread.start();
  55.         }
  56.     }
  57.  
  58.     public void stop() {
  59.         //Stop the animating thread.
  60.         animatorThread = null;
  61.     }
  62.  
  63.     public boolean mouseDown(Event e, int x, int y) {
  64.         if (frozen) {
  65.             frozen = false;
  66.             start();
  67.         } else {
  68.             frozen = true;
  69.             stop();
  70.         }
  71.         return true;
  72.     }
  73.  
  74.     public void run() {
  75.         //Just to be nice, lower this thread's priority
  76.         //so it can't interfere with other processing going on.
  77.         Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
  78.  
  79.         //Remember the starting time.
  80.         long startTime = System.currentTimeMillis();
  81.  
  82.         //This is the animation loop.
  83.         while (Thread.currentThread() == animatorThread) {
  84.             //Advance the animation frame.
  85.             frameNumber++;
  86.  
  87.             //Display it.
  88.             repaint();
  89.  
  90.             //Delay depending on how far we are behind.
  91.             try {
  92.                 startTime += delay;
  93.                 Thread.sleep(Math.max(0, 
  94.                                       startTime-System.currentTimeMillis()));
  95.             } catch (InterruptedException e) {
  96.                 break;
  97.             }
  98.         }
  99.     }
  100.  
  101.     //Draw the current frame of animation.
  102.     public void paint(Graphics g) {
  103.         g.drawString("Frame " + frameNumber, 0, 30);
  104.     }
  105. }
  106.